﻿using System;
using System.Xml;
using System.Data;
using System.IO;
using System.Diagnostics;
using System.Text;
using LibKetoan;

namespace Ketoan
{
	/// <summary>
	/// Summary description for frmBangcandoitk_class.
	/// </summary>
	public class frmBangcandoitk_class
	{
		#region Khai bao thanh phan su dung trong class
		bool flag_dsnull;
		string sql="";
		System.Data.DataSet ds_Data;
		#endregion

		public frmBangcandoitk_class()
		{
		}
		//

		private System.Data.DataSet f_Loadtatcataikhoan(LibKetoan.AccessData a)
		{
			//Load tat ca danh muc tai khoan vao DataSet.
			System.Data.DataSet ds_Dmtk=new System.Data.DataSet();
			sql=  "select id,stk,ttk,tknb,sp,hethong,tinhchat,mlns,ctmlns,c,bcdtk,0 daduyet,0 dacapnhat,";
			sql+=" 0 sdno,0 sdco,0 fsno,0 fsco,0 luykeno,0 luykeco,0 cuoikyno,0 cuoikyco,0 loaitk,";
			sql+=" 0 sapxep from a_dmtk where sudung=1 order by stk";
			
			try
			{
				ds_Dmtk=a.get_data(sql).Copy();
				if ((ds_Dmtk==null || ds_Dmtk.Tables[0].Rows.Count<=0)) flag_dsnull=true;
				//ds_Data.WriteXml("..\\..\\xml_data\\KT_Bangtonghopchungtu.xml",XmlWriteMode.WriteSchema);
			}
			catch
			{
				flag_dsnull=true;
			}
			if (flag_dsnull) return null;
			else return ds_Dmtk;
		}

		public System.Data.DataSet f_Tinhsodudauky(LibKetoan.AccessData a,string s_IDchuong,string s_IDlk,string s_IDhoatdong,string s_IDnhombc,string s_Tu,string s_Den,string s_yy)
		{
			//Tinh so du cho cac tai khoan chi tiet.
			System.Data.DataSet ds_Sodu=new DataSet();
			string s_Fsno="fsno"+s_Tu, s_Fsco="fsco"+s_Tu; 
			for (int i=int.Parse(s_Tu)+1; i<=int.Parse(s_Den); i++)
			{
				s_Fsno+="+fsno"+i.ToString().PadLeft(2,'0');
				s_Fsco+="+fsco"+i.ToString().PadLeft(2,'0');
			}

			string s_Luykeno="0", s_Luykeco="0";
			if (int.Parse(s_Tu)>1)
			{
				s_Luykeno=" sum(fsno01"; s_Luykeco=" sum(fsco01";
				for (int i=2; i<int.Parse(s_Tu); i++)
				{
					s_Luykeno+="+fsno"+i.ToString().PadLeft(2,'0');
					s_Luykeco+="+fsco"+i.ToString().PadLeft(2,'0');
				}
				s_Luykeno+=")"; s_Luykeco+=")";
			}

			sql=  "select stka,sum(sdno"+s_Tu+") sdno, sum(sdco"+s_Tu+") sdco, sum("+s_Fsno+") fsno, sum("+s_Fsco+") fsco,";
			sql+= s_Luykeno+" luykeno,"+s_Luykeco+" luykeco";
			sql+=" from a_sd"+s_yy+" where stka is not null";
			if (s_IDchuong!="") sql+=" and chuong='"+s_IDchuong+"'";
			if (s_IDlk!="") sql+=" and lk='"+s_IDlk+"'";
			if (s_IDhoatdong!="") sql+=" and hoatdong='"+s_IDhoatdong+"'";
			if (s_IDnhombc!="") sql+=" and idbc='"+s_IDnhombc+"'";
			sql+=" group by stka order by stka";

			try
			{
				ds_Sodu=a.get_data(sql).Copy();
				if ((ds_Sodu==null || ds_Sodu.Tables[0].Rows.Count<=0)) flag_dsnull=true;
				//ds_Data.WriteXml("..\\..\\xml_data\\KT_Bangtonghopchungtu.xml",XmlWriteMode.WriteSchema);
			}
			catch
			{
				flag_dsnull=true;
			}
			if (flag_dsnull) return null;
			else return ds_Sodu;
		}

		private System.Data.DataRow f_Tinhtaikhoangoc (System.Data.DataSet ds_Data,string s_Stkduyet,int i_Tinhchat,ref bool b_Null) 
		{
			DataSet ds_Tkgoc=ds_Data.Clone();
			decimal d_Soduno=0, d_Soduco=0, d_Fsno=0, d_Fsco=0, d_Luykeno=0, d_Luykeco=0, d_Cuoikyno=0, d_Cuoikyco=0, d_Sumall=0;

			try
			{
				foreach (DataRow dr in ds_Data.Tables[0].Select("stk like '"+s_Stkduyet+"%'","stk"))
				{
					if (dr!=null)
					{
						d_Soduno+=decimal.Parse(dr["sdno"].ToString());
						d_Soduco+=decimal.Parse(dr["sdco"].ToString());
						d_Fsno+=decimal.Parse(dr["fsno"].ToString());
						d_Fsco+=decimal.Parse(dr["fsco"].ToString());
						d_Luykeno+=decimal.Parse(dr["luykeno"].ToString());
						d_Luykeco+=decimal.Parse(dr["luykeco"].ToString());
					}
				}
			}
			catch{}	;
			switch (i_Tinhchat)
			{ 
				case 0:
					d_Cuoikyno=(d_Soduno+d_Fsno)- Math.Min((d_Soduno+d_Fsno),(d_Soduco+d_Fsco));
					d_Cuoikyco=(d_Soduco+d_Fsco)- Math.Min((d_Soduno+d_Fsno),(d_Soduco+d_Fsco));
					break;
				case 1:
					d_Cuoikyno=(d_Soduno-d_Soduco)+d_Fsno-d_Fsco;
					d_Cuoikyco=0;
					break;
				case 2:
					d_Cuoikyno=0;
					d_Cuoikyco=(d_Soduco-d_Soduno)+d_Fsco-d_Fsno;
					break;
			}

			System.Data.DataRow dr_Tkgoc = ds_Tkgoc.Tables[0].NewRow();
			dr_Tkgoc["sdno"]=d_Soduno;
			dr_Tkgoc["sdco"]=d_Soduco;
			dr_Tkgoc["fsno"]=d_Fsno;
			dr_Tkgoc["fsco"]=d_Fsco;
			dr_Tkgoc["luykeno"]=d_Luykeno;
			dr_Tkgoc["luykeco"]=d_Luykeco;
			dr_Tkgoc["cuoikyno"]=d_Cuoikyno;
			dr_Tkgoc["cuoikyco"]=d_Cuoikyco;

			ds_Tkgoc.Tables[0].Rows.Add(dr_Tkgoc);
			ds_Tkgoc.AcceptChanges();
			d_Sumall=d_Soduno+d_Soduco+d_Fsno+d_Fsco+d_Luykeno+d_Luykeco+d_Cuoikyno+d_Cuoikyco;
			if (d_Sumall!=0) b_Null=false;
			else b_Null=true;

			if (ds_Tkgoc.Tables[0].Rows.Count>0) return ds_Tkgoc.Tables[0].Rows[0];
			else return null;
		}

		public System.Data.DataSet f_Bangcandoitaikhoan(LibKetoan.AccessData a,string s_IDchuong,string s_IDlk,string s_IDhoatdong,string s_IDnhombc,string s_Tu,string s_Den,string s_yy,bool b_Chkchitiet)
		{
			//Tinh so du cho cac tai khoan chi tiet.
			try
			{
				ds_Data=f_Loadtatcataikhoan(a).Copy();
				System.Data.DataSet ds_Sodu=f_Tinhsodudauky(a,s_IDchuong,s_IDlk,s_IDhoatdong,s_IDnhombc,s_Tu,s_Den,s_yy).Copy();

				while (ds_Data.Tables[0].Select("daduyet=0 and c=1","stk").Length>0)
				{
					System.Data.DataRow dr=ds_Data.Tables[0].Select("daduyet=0 and c=1","stk")[0];
					decimal d_Soduno=0, d_Soduco=0, d_Fsno=0, d_Fsco=0, d_Luykeno=0, d_Luykeco=0, d_Cuoikyno=0, d_Cuoikyco=0, d_Sumall=0;
					string s_IDstkduyet=dr["id"].ToString().Trim();
					try
					{
						System.Data.DataRow dr_Sodu=a.getrowbyid(ds_Sodu.Tables[0],"stka='"+s_IDstkduyet+"'");
						if (dr_Sodu!=null)
						{
							d_Soduno=decimal.Parse(dr_Sodu["sdno"].ToString());
							d_Soduco=decimal.Parse(dr_Sodu["sdco"].ToString());
							d_Fsno=decimal.Parse(dr_Sodu["fsno"].ToString());
							d_Fsco=decimal.Parse(dr_Sodu["fsco"].ToString());
							d_Luykeno=decimal.Parse(dr_Sodu["luykeno"].ToString())+d_Fsno;
							d_Luykeco=decimal.Parse(dr_Sodu["luykeco"].ToString())+d_Fsco;
							switch (int.Parse(dr["tinhchat"].ToString()))
							{ 
								case 0:
									d_Cuoikyno=(d_Soduno+d_Fsno)- Math.Min((d_Soduno+d_Fsno),(d_Soduco+d_Fsco));
									d_Cuoikyco=(d_Soduco+d_Fsco)- Math.Min((d_Soduno+d_Fsno),(d_Soduco+d_Fsco));
									break;
								case 1:
									d_Cuoikyno=(d_Soduno-d_Soduco)+d_Fsno-d_Fsco;
									d_Cuoikyco=0;
									break;
								case 2:
									d_Cuoikyno=0;
									d_Cuoikyco=(d_Soduco-d_Soduno)+d_Fsco-d_Fsno;
									break;
							}
						}
					}
					catch{}	;
					d_Sumall=d_Soduno+d_Soduco+d_Fsno+d_Fsco+d_Luykeno+d_Luykeco+d_Cuoikyno+d_Cuoikyco;

					dr["sdno"]=d_Soduno;
					dr["sdco"]=d_Soduco;
					dr["fsno"]=d_Fsno;
					dr["fsco"]=d_Fsco;
					dr["luykeno"]=d_Luykeno;
					dr["luykeco"]=d_Luykeco;
					dr["cuoikyno"]=d_Cuoikyno;
					dr["cuoikyco"]=d_Cuoikyco;
					dr["daduyet"]=1;
					if(d_Sumall!=0) dr["dacapnhat"]=1;
					else dr["dacapnhat"]=0;
					//ds_Data.Tables[0].Rows.Remove(dr);
				}
				ds_Data.AcceptChanges();
				try { if (ds_Sodu!=null) { ds_Sodu.Dispose(); GC.Collect();} } 
				catch{}

				//Tinh phat sinh cho cac tai khoan goc.
				while (ds_Data.Tables[0].Select("daduyet=0 and c=0","stk").Length>0)
				{
					System.Data.DataRow dr=ds_Data.Tables[0].Select("daduyet=0 and c=0","stk")[0];
					string s_Stkduyet=dr["stk"].ToString().Trim();
					int i_Tinhchat=int.Parse(dr["tinhchat"].ToString().Trim());
					bool b_Null=false;
					try
					{
						System.Data.DataRow dr_Sodu=f_Tinhtaikhoangoc(ds_Data,s_Stkduyet,i_Tinhchat,ref b_Null);
						if (dr_Sodu!=null)
						{
							dr["sdno"]=decimal.Parse(dr_Sodu["sdno"].ToString());
							dr["sdco"]=decimal.Parse(dr_Sodu["sdco"].ToString());
							dr["fsno"]=decimal.Parse(dr_Sodu["fsno"].ToString());
							dr["fsco"]=decimal.Parse(dr_Sodu["fsco"].ToString());
							dr["luykeno"]=decimal.Parse(dr_Sodu["luykeno"].ToString());
							dr["luykeco"]=decimal.Parse(dr_Sodu["luykeco"].ToString());
							dr["cuoikyno"]=decimal.Parse(dr_Sodu["cuoikyno"].ToString());
							dr["cuoikyco"]=decimal.Parse(dr_Sodu["cuoikyco"].ToString());
						}
						dr["daduyet"]=1;
						if (b_Null) dr["dacapnhat"]=0;
						else dr["dacapnhat"]=1;
					}
					catch{}	;

				}
				ds_Data.AcceptChanges();
			
				if (b_Chkchitiet) 
				{
					while(ds_Data.Tables[0].Select("bcdtk=0","stk").Length>0)
						ds_Data.Tables[0].Rows.Remove(ds_Data.Tables[0].Select("bcdtk=0","stk")[0]);
				}
				while (ds_Data.Tables[0].Select("dacapnhat=0","stk").Length>0)
					ds_Data.Tables[0].Rows.Remove(ds_Data.Tables[0].Select("dacapnhat=0","stk")[0]);
				ds_Data.AcceptChanges();

				try
				{
					if ((ds_Data==null || ds_Data.Tables[0].Rows.Count<=0)) flag_dsnull=true;
					ds_Data.WriteXml("..\\xml\\KT_Bangtonghopchungtu1.xml",XmlWriteMode.WriteSchema);
				}
				catch
				{
					flag_dsnull=true;
				}
				if (flag_dsnull) return null;
				else return ds_Data;
			}
			catch{}	;
			return null;
		}

		#region Dong tat ca cac ket noi va dataset khi ket thuc form
		public void Dispose()
		{
			try
			{
				ds_Data.Dispose();
				GC.Collect();
			}
			catch{};
		}
		#endregion
	}
}

